//
//  ImageLabelButton.m
//  ImageLabelButton
//
//  Created by nhn on 12/29/14.
//  Copyright (c) 2014 CNUIC. All rights reserved.
//

#import "ImageLabelButton.h"

@implementation ImageLabelButton

@synthesize imageView = _imageView;
@synthesize titleLabel = _titleLabel;
@synthesize animateImage = _animateImage;
@synthesize tintColor = _tintColor;

- (instancetype)init {
    if (self = [super init]) {
        [self initViews];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self initViews];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        [self initViews];
    }
    return self;
}

- (void)initViews {
    self.imageView = [[UIImageView alloc] init];
    self.imageView.translatesAutoresizingMaskIntoConstraints = NO;
    [self addSubview:self.imageView];
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.adjustsFontSizeToFitWidth = YES;
    [self addSubview:self.titleLabel];
    
    self.titleLabel.textColor = [UIColor whiteColor];
    
//    self.imageView.backgroundColor = [UIColor yellowColor];
//    self.titleLabel.backgroundColor = [UIColor blueColor];
    
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-10-[_imageView]-[_titleLabel]-|" options:NSLayoutFormatAlignAllCenterY metrics:nil views:@{@"_imageView":self.imageView, @"_titleLabel":self.titleLabel}]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-6-[imageView(>=20)]-6-|" options:0 metrics:nil views:@{@"imageView":self.imageView}]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-6-[titleLabel]-6-|" options:0 metrics:nil views:@{@"titleLabel":self.titleLabel}]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.imageView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.imageView attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0]];
    
    [self addTarget:self action:@selector(toucheDown:) forControlEvents:UIControlEventTouchDown];
    [self addTarget:self action:@selector(toucheUp:) forControlEvents:UIControlEventTouchUpInside];
    
    self.clipsToBounds = NO;
    
    self.layer.borderColor = [UIColor whiteColor].CGColor;
    self.layer.borderWidth = 1;
}

/*
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesCancelled:touches withEvent:event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesMoved:touches withEvent:event];
}
 */

- (void)toucheDown:(id)sender {
    [self circleAnimation];
}

- (void)toucheUp:(id)sender {
//    [self circleAnimation];
}

- (void)circleAnimation {
    /*
    UIImage* image = self.animateImage;
    if (image) {
        UIImageView* imgView = [[UIImageView alloc] initWithFrame:self.imageView.frame];
        imgView.image = image;
        [self insertSubview:imgView belowSubview:self.imageView];
        
        imgView.alpha = 0.8;
        [UIView animateWithDuration:0.7
                              delay:0.0
                            options:UIViewAnimationOptionCurveEaseIn
                         animations:^{
                             imgView.transform = CGAffineTransformMakeScale(2, 2);
                             imgView.alpha = 0.0;
                         }
                         completion:^(BOOL finished) {
                             [imgView removeFromSuperview];
                         }];
    }
     */
    
    UIView* animateView = [[UIView alloc] initWithFrame:self.bounds];
    [self addSubview:animateView];
    animateView.backgroundColor = [UIColor clearColor];
    animateView.layer.borderColor = self.layer.borderColor;
    animateView.layer.borderWidth = self.layer.borderWidth;
    
    animateView.alpha = 0.8;
    [UIView animateWithDuration:0.7
                          delay:0.0
                        options:UIViewAnimationOptionCurveEaseIn
                     animations:^{
                         animateView.transform = CGAffineTransformMakeScale(1.4, 2.8);
                         animateView.alpha = 0.0;
                     }
                     completion:^(BOOL finished) {
                         [animateView removeFromSuperview];
                     }];
}

- (void)setTintColor:(UIColor *)tColor
{
    _tintColor = tColor;
    
    self.titleLabel.textColor = tColor;
    self.layer.borderColor = tColor.CGColor;
}

- (UIColor *)tintColor {
    return _tintColor;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
